﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Esponce.Common;
using Esponce.QRCode;

namespace Esponce.Models
{
	public class TrackListViewModel : INotifyPropertyChanged
	{
		#region Constructor
		public TrackListViewModel()
		{
#if DEBUG
			if (Settings.IsDesignMode)
			{
				this.Skip = 10;
				this.Take = 20;
				this.Since = DateTime.UtcNow.AddMonths(-1);
				this.Until = DateTime.UtcNow;

				this.Items = new List<TrackListItem>()
				{
					new TrackListItem()
					{
					  Id = "99",
					  Name = "Lorem Ipsum",
					  Info = "comment...",
						//Address = "Nowhere",
						//Longitude = 10,
						//Latitude = 10,
						//Count = 100,
						//NewCount = 10,
					  Created = DateTime.UtcNow,
						//OriginalUrl = "http://www.example.com",
						//TrackUrl = "http://goo.by/example",
					  //QRCodeImageUrl = "",
					},
					new TrackListItem()
					{
					  Id = "ebf4e007447846f292234ea185a6b95d",
						Type = "Campaign",
					  Name = "Lorem Ipsum",
					  Info = "comment...",
					  Created = DateTime.UtcNow,
					},
					new TrackListItem()
					{
					  Id = "101",
						Type = "QR Code",
					  Name = "Lorem Ipsum",
					  Info = "comment...",
					  Created = DateTime.UtcNow,
					}
				};
			}
#endif
		}
		#endregion

		#region Public properties
		public int? Skip
		{
			get
			{
				return this.skip;
			}
			set
			{
				if (this.skip != value)
				{
					this.skip = value;
					OnPropertyChanged("Skip");
				}
			}
		}
		private int? skip;

		public int? Take
		{
			get
			{
				return this.take;
			}
			set
			{
				if (this.take != value)
				{
					this.take = value;
					OnPropertyChanged("Take");
				}
			}
		}
		private int? take;

		public DateTime? Since
		{
			get
			{
				return this.since;
			}
			set
			{
				if (this.since != value)
				{
					this.since = value;
					OnPropertyChanged("Since");
				}
			}
		}
		private DateTime? since;

		public DateTime? Until
		{
			get
			{
				return this.until;
			}
			set
			{
				if (this.until != value)
				{
					this.until = value;
					OnPropertyChanged("Until");
				}
			}
		}
		private DateTime? until;

		public IEnumerable<TrackListItem> Items
		{
			get
			{
				return this.items;
			}
			set
			{
				if (this.items != value)
				{
					this.items = value;
					OnPropertyChanged("Items");
				}
			}
		}
		private IEnumerable<TrackListItem> items;

		public bool IsLoading
		{
			get
			{
				return this.isLoading;
			}
			set
			{
				if (this.isLoading != value)
				{
					this.isLoading = value;
					OnPropertyChanged("IsLoading");
				}
			}
		}
		private bool isLoading;
		#endregion

		#region Public methods
		public void GetTrackList()
		{
			try
			{
				//Start the loading animation for better UX
				this.IsLoading = true;

				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("GET " + client.UriBuilder.BuildTrackListUrl());

				client.GetTrackListCompleted += (sender, e) =>
				{
					//Stop the loading animation
					this.IsLoading = false;

					try
					{
						if (e.Error != null)
						{
							throw e.Error;
						}

						if (e.Result == null)
						{
							throw new Exception("Result is missing!");
						}

						var items = new List<TrackListItem>();

						if (e.Result.Campaigns != null)
						{
							foreach (var campaign in e.Result.Campaigns)
							{
								var item = new TrackListItem();
								item.Id = campaign.Id;
								item.Type = "Campaign";
								item.Name = campaign.Name;
								item.Info = campaign.Description;
								item.Created = campaign.Created;
								item.IsCampaign = true;
								item.IsAvailable = true;
								item.Scans = -1;
								items.Add(item);
							}
						}

						if (e.Result.QRCodes != null)
						{
							foreach (var qrcode in e.Result.QRCodes)
							{
								var item = new TrackListItem();
								item.Id = qrcode.Id;
								item.Type = "QR Code";
								item.Name = qrcode.Name;
								item.Info = qrcode.Comment;
								item.Created = qrcode.Created;
								item.IsCampaign = false;
								item.IsAvailable = (qrcode.IsAvailable == true);
								item.Scans = qrcode.Scans;
								items.Add(item);
							}
						}

						this.Items = items;

						App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
					}
					catch (Exception ex)
					{
						//Report error to the console
						App.Logger.Log(ex);
					}
				};
				client.GetTrackListAsync();
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}
		#endregion

		#region INotifyPropertyChanged Members
		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}
